package com.ssau.expert.app;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Author: Sergey Saiyan
 * Created: 03.10.13 14:11
 */
public class AppConfig {

    public static final String CONFIG_PATH = "C:\\ExpertSystemDefaults.txt";
    public static final File CONFIG_FILE = new File(CONFIG_PATH);

    protected static File defaultQuestionsFile;
    protected static File defaultRulesFile;

    protected static String logFilePath = "D:\\ddd\\ExpertLog.txt";

    @SuppressWarnings("unchecked")
    public static void initDefault() {
        try {
            List<String> lines = FileUtils.readLines(CONFIG_FILE);
            String defQuestions = lines.get(0);
            String defRules = lines.get(1);
            String defLogFile = lines.get(2);

            defaultQuestionsFile = new File(defQuestions);
            defaultRulesFile = new File(defRules);
            logFilePath = defLogFile;
        } catch (IOException ex) {
            System.out.println("Catch IOException in reading default file");
        } catch (Exception ex) {
            System.out.println("Default file is invalid");
        }
    }

    public static File getDefaultQuestionsFile() {
        return defaultQuestionsFile;
    }

    @SuppressWarnings("unchecked")
    public static void setDefaultQuestionsFile(File defaultQuestionsFile) {
        AppConfig.defaultQuestionsFile = defaultQuestionsFile;
        String qPath = defaultQuestionsFile.getAbsolutePath();
        try {
            List<String> lines = FileUtils.readLines(CONFIG_FILE);
            String rPath = lines.get(1);
            List<String> newLines = new ArrayList<>(2);
            newLines.add(qPath);
            newLines.add(rPath);
            FileUtils.writeLines(CONFIG_FILE, newLines);
        } catch (IOException ex) {
            System.out.println("IO Exception while saving new QuestionFile");
        }
    }

    public static File getDefaultRulesFile() {
        return defaultRulesFile;
    }

    @SuppressWarnings("unchecked")
    public static void setDefaultRulesFile(File defaultRulesFile) {
        AppConfig.defaultRulesFile = defaultRulesFile;
        String rPath = defaultRulesFile.getAbsolutePath();
        try {
            List<String> lines = FileUtils.readLines(CONFIG_FILE);
            String qPath = lines.get(0);
            List<String> newLines = new ArrayList<>(2);
            newLines.add(qPath);
            newLines.add(rPath);
            FileUtils.writeLines(CONFIG_FILE, newLines);
        } catch (IOException ex) {
            System.out.println("IO Exception while saving new QuestionFile");
        }
    }

    public static String getLogFilePath() {
        return logFilePath;
    }

    public static void setLogFilePath(String logFilePath) {
        AppConfig.logFilePath = logFilePath;
    }
}
